package com.neu.adapter;

import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.neu.R;
import com.neu.bean.News;
import com.neu.util.VolleyUtil;
import com.neu.view.App;

import java.util.List;

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {

    private List<News.RowsBean> list;

    public NewsAdapter(List<News.RowsBean> list){
        this.list=list;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(App.context).inflate(R.layout.dashboard_news_adapter_layout,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //获得一条新闻
        News.RowsBean r=list.get(position);
        holder.txtReadNum.setText(holder.txtReadNum.getText().toString()+r.getReadNum());
        holder.txtNewsDate.setText(holder.txtNewsDate.getText().toString()+r.getPublishDate());
        holder.txtNewsContent.setText(Html.fromHtml(r.getContent(),Html.FROM_HTML_MODE_COMPACT));
        holder.txtLikeNum.setText(holder.txtLikeNum.getText().toString()+r.getLikeNum());
        holder.txtNewsTitle.setText(r.getTitle());
        //获得图片
        Glide.with(App.context).load(VolleyUtil.BASE_URL+r.getCover()).into(holder.imgNews);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        //获得图片对象
        private ImageView imgNews;
        private TextView txtNewsTitle;
        private TextView txtNewsContent;
        private TextView txtNewsDate;
        private TextView txtLikeNum;
        private TextView txtReadNum;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imgNews=itemView.findViewById(R.id.imgNews);
            txtNewsTitle=itemView.findViewById(R.id.txtNewsTitle);
            txtLikeNum=itemView.findViewById(R.id.txtLikeNum);
            txtNewsContent=itemView.findViewById(R.id.txtNewsContent);
            txtNewsDate=itemView.findViewById(R.id.txtNewsDate);
            txtReadNum=itemView.findViewById(R.id.txtReadNum);
        }
    }
}
